﻿using Mimas.Core.Current;
using Mimas.Framework.Shared.Data.DataTransferObjects;
using System;
using System.Linq;

namespace Mimas.Framework.Server.DomainObjects.Mapping
{
    public static class MenuItemMapping
    {
        public static MenuItems Map(MenuItemDTO data)
        {
            MenuItems newItem = new MenuItems();

            newItem.IsActive = data.IsActive;
            newItem.Id = data.Id;
            newItem.Name = data.Name;
            newItem.URL = data.URL;

            if (data.Id == 0)
            {
                newItem.CreatedBy = UserInfo.CurrentUser.UserId;
                newItem.CreatedOn = DateTime.Now;
            }
            else
            {
                newItem.ModifiedBy = UserInfo.CurrentUser.UserId;
                newItem.ModifiedOn = DateTime.Now;
            }

            return newItem;
        }

        public static MenuItemDTO Map(MenuItems data)
        {
            MenuItemDTO newItem = new MenuItemDTO();

            newItem.IsActive = data.IsActive;
            newItem.Id = data.Id;
            newItem.Name = data.Name;
            newItem.URL = data.URL;

            return newItem;
        }

        public static MenuItems[] Map(MenuItemDTO[] data)
        {
            if (data == null || !data.Any()) return null;
            return data.Where(x => x != null).Select(x => Map(x)).ToArray();
        }

        public static MenuItemDTO[] Map(MenuItems[] data)
        {
            if (data == null || !data.Any()) return null;
            return data.Where(x => x != null).Select(x => Map(x)).ToArray();
        }
    }
}
